package net.mihome.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;

import net.mihome.app.AppContext;
import net.mihome.app.R;
import net.mihome.app.bean.URLs;
import net.mihome.app.bean.net.ResponsePojo;
import net.mihome.app.ui.view.MihomeGoodsView;
import net.mihome.app.ui.view.MyViewLinster;
import net.mihome.app.utils.MiAsyncHttpResponseHandler;
import net.mihome.app.utils.RequestParamsFactory;

import org.apache.http.Header;

public class GoodsMainActivity extends BaseActivity {

	MihomeGoodsView mihomeGoodsView;
    TextView buyCarSum;
    View viewLayout;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.goods_main);
        mihomeGoodsView=(MihomeGoodsView) findViewById(R.id.mifuwu_goods_listview);
        buyCarSum= (TextView) findViewById(R.id.buy_car_sum);
        viewLayout=findViewById(R.id.buy_car_layout);
        mihomeGoodsView.setMyViewLinster(new MyViewLinster() {
			
			@Override
			public void onLoadComplete() {
				// TODO Auto-generated method stub
				refresh();
			}
			
			@Override
			public void onBeforeLoad() {
				// TODO Auto-generated method stub
			}
		});
        viewLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent Intent = new Intent(GoodsMainActivity.this, BuyCarActivity.class);
                startActivity(Intent);
            }
        });
        ((Button)findViewById(R.id.button_to_buycar)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent Intent = new Intent(GoodsMainActivity.this, BuyCarActivity.class);
                startActivity(Intent);
            }
        });
        refresh();
        mihomeGoodsView.initData(false);
	}

    @Override
    protected void onResume() {
        refresh();
        super.onResume();
    }

    /**
     * 刷新购物车的数量
     * */
    public void refresh(){
        RequestParams requestParams= RequestParamsFactory.newInstance(AppContext.getInstance());
        AppContext.ASYNC_HTTP_CLIENT.get(this, URLs.MIHOME_GET_GOODS_BUYCAR_COUNT,requestParams,new MiAsyncHttpResponseHandler<String>() {
            @Override
            public void onSuccess(int arg0, Header[] arg1, ResponsePojo<String> responsePojo) {
                String numStrig=responsePojo.getData();
                Long num=Long.parseLong(numStrig);
                if(num>0){
                    buyCarSum.setVisibility(View.VISIBLE);
                    buyCarSum.setText(num.intValue()+"");
                }else{
                    buyCarSum.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFailure(int arg0, Header[] arg1, Throwable arg3) {

            }
        });
    }

}
